/******************************************************************************
 * Copyright (C) 2008 Adam Parker. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.beans.XMLEncoder;
import javax.swing.event.ListSelectionEvent;

// The main class in the grade keeping application that creates the GUI and keeps track of data
public class Main extends JFrame implements MouseListener, ActionListener
{
	// global variables used for various operations
	FileOptions filOps = new FileOptions();
	GridBagLayout lotManager;
	GridBagConstraints gbcConstraint;
	private int intRows = 100;
	private int intCols = 20;
	private int intCurRow = -1;
	private int intCurCol = -1;
	private int intCurStu = -1;
	private JComboBox cbxComplete, cbxCategory;	
	private JEditorPane editorPane;
	final private JFileChooser fc = new JFileChooser();
	private JMenuBar mubMain;
	private JPanel pnlMain, pnlSub, pnlStudent, pnlAssignment, pnlGrading;
	final private JTable table = new JTable(intRows, intCols)
	{
		public void valueChanged(ListSelectionEvent e) 
		{
			super.valueChanged(e);
			checkSelection(false);
		}
    };
	final private JTable tblNames = new JTable(intRows, 1)
	{
		public void valueChanged(ListSelectionEvent e)
		{
			super.valueChanged(e);
			checkSelection(true);
		}
	};
	private JTextField txtLate, txtNumber, txtEmail, txtAssignment, txtPoints, txtDueDate, txtCategory;
	private TableColumnModel columnModel;
	private String strSaveFile = "";
	private XML xmlSave = null;
	private CellData[][] cldGradeInfo = new CellData[intRows+1][intCols];
	private StudentData[] sdaStudentInfo = new StudentData[intRows+1];
	private AssignmentData[] asdAssignmentInfo = new AssignmentData[intCols];
	private GradeCalculator calculator;
	
	// Main constructor that initializes data structures and draws the GUI
	public Main()
	{
		// Set properties of JFrame
		super("Grades");
		setSize(1000,700);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Add Window Listener and define action for when the window is closing
		addWindowListener(new WindowAdapter() 
    	{
    		public void windowClosing(WindowEvent e) 
    		{
				// NOTE: More functionality to be added here so it prompts you to save file
    			System.exit(0);
    		}
		});

		// Set the layout manager of the JFrame
		lotManager = new GridBagLayout();
		gbcConstraint = new GridBagConstraints();
		gbcConstraint.fill = GridBagConstraints.BOTH;
		setLayout(lotManager);
		
		// Get the container
		Container content = getContentPane();
		
		// Initialize the main panel and set it to have a Box Layout
		pnlMain = new JPanel();
		pnlMain.setLayout(new BoxLayout(pnlMain, BoxLayout.Y_AXIS));
		pnlMain.setBorder(null);
		
		// Initialize objects used to keep track of extra information
		for(int i = 0; i < intRows; i++)
			for(int j = 0; j < intCols; j++)
				cldGradeInfo[i][j] = new CellData();				
		for(int i = 0; i < intRows; i++)
			sdaStudentInfo[i] = new StudentData();			
		for(int i = 0; i < intCols; i++)
			asdAssignmentInfo[i] = new AssignmentData();
		
		// Create main menu for "File" and "About" menus
		mubMain = new JMenuBar();
		mubMain.setBorder(BorderFactory.createMatteBorder(0,0,2,0,Color.BLACK));
		JMenu mnuMain = new JMenu("File");
		JMenuItem jmiOpen = new JMenuItem("Open");
		JMenuItem jmiSave = new JMenuItem("Save");
		JMenuItem jmiSaveAs = new JMenuItem("Save As");
		JMenuItem jmiExit = new JMenuItem("Exit");
		JMenuItem jmiCalc = new JMenuItem("Load Class");
		jmiOpen.addActionListener(this);
		jmiSave.addActionListener(this);
		jmiExit.addActionListener(this);
		jmiSaveAs.addActionListener(this);
		jmiCalc.addActionListener(this);
		jmiOpen.setActionCommand("Open");
		jmiSave.setActionCommand("Save");
		jmiSaveAs.setActionCommand("Save As");
		jmiExit.setActionCommand("Exit");
		jmiCalc.setActionCommand("LoadClass");
		mnuMain.add(jmiOpen);
		mnuMain.add(jmiSave);
		mnuMain.add(jmiSaveAs);
		mnuMain.add(jmiCalc);
		mnuMain.add(jmiExit);
		mubMain.add(mnuMain);
		setJMenuBar(mubMain);
		
		// Set properties for the main table
		table.setCellSelectionEnabled(true);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.addMouseListener(this);
		
		// Set the table header for the main table and initialize header values
		columnModel = table.getColumnModel();
		table.setTableHeader(new EditableHeader(columnModel));		
		for(int i = 0; i < intCols; i++)
			columnModel.getColumn(i).setHeaderValue(" ");
		
		// Add mouse listener to the names table and set properties for the names table
		tblNames.addMouseListener(this);
		tblNames.setPreferredScrollableViewportSize(new Dimension(100, 700));
		TableColumn column = tblNames.getColumnModel().getColumn(0);
		column.setPreferredWidth(100);
		
		// Initialize an XML object that will be used to save data
		xmlSave = new XML(table, tblNames, cldGradeInfo, sdaStudentInfo, asdAssignmentInfo, columnModel);
		
		// Initialize the scroll pane for the names table
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setRowHeaderView(tblNames);
		
		// Create labels for GUI
		JLabel lblRow = new JLabel("Row");
		JLabel lblColumn = new JLabel("Column");
		JLabel lblCell = new JLabel("Cell");
		JLabel lblNumber = new JLabel("Student Number:");
		JLabel lblEmail = new JLabel("Email:");
		JLabel lblAssignment = new JLabel("Assignment: ");
		JLabel lblPoints = new JLabel("Points: ");
		JLabel lblDueDate = new JLabel("Due Date :");
		JLabel lblCategory = new JLabel("Category:");
		JLabel lblLate = new JLabel("Days Late:");
		JLabel lblComplete = new JLabel("Complete?");
		JLabel lblInfo = new JLabel("Extra Info:");

		// Add text fields, combo boxes, and editor panes for data entry
		txtNumber = new JTextField(100);
		txtEmail = new JTextField(100);
		txtAssignment = new JTextField(100);
		txtPoints = new JTextField(100);
		txtDueDate = new JTextField(100);
		txtCategory = new JTextField(100);
		txtLate = new JTextField(100);
		Object[] lstComplete = {"Yes", "No"};
		cbxComplete = new JComboBox(lstComplete);
		
		editorPane = new JEditorPane();
		editorPane.setEditable(true);
		JScrollPane editorScrollPane = new JScrollPane(editorPane);
		editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		editorScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);						
		
		// Set properties for Row, Column, and Cell labels
		lblRow.setFont(new Font("SansSerif", Font.BOLD, 12));
		lblColumn.setFont(new Font("SansSerif", Font.BOLD, 12));
		lblCell.setFont(new Font("SansSerif", Font.BOLD, 12));
		lblRow.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
		lblColumn.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
		lblCell.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
		
		// Create inidividual panels for information and set their layout to a Grid Bag Layout
		pnlStudent = new JPanel();
		pnlAssignment = new JPanel();
		pnlGrading = new JPanel();
		pnlStudent.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
		pnlAssignment.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
		pnlGrading.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
		GridBagLayout gblStudentManager = new GridBagLayout();
		GridBagLayout gblAssignmentManager = new GridBagLayout();
		GridBagLayout gblGradingManager = new GridBagLayout();
		pnlStudent.setLayout(gblStudentManager);
		pnlAssignment.setLayout(gblAssignmentManager);
		pnlGrading.setLayout(gblGradingManager);
		
		// Add scroll pane to the main panel
		pnlMain.add(scrollPane);
		
		// Set Grid Bag Layout properties
		gbcConstraint.weightx = 1;
		gbcConstraint.weighty = 1;
		gbcConstraint.gridheight = 1;
		gbcConstraint.anchor = GridBagConstraints.CENTER;
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		
		// Set constraints for panle main and add it to the content pane
		lotManager.setConstraints(pnlMain, gbcConstraint);
		content.add(pnlMain);
		
		// Set weighty to 0 so dashboard items will not grow vertically when windows resize
		gbcConstraint.weighty = 0;
		
		// Add items to the content pane
		gbcConstraint.gridwidth = 1;
		lotManager.setConstraints(lblRow, gbcConstraint);
		content.add(lblRow);
		lotManager.setConstraints(lblColumn, gbcConstraint);
		content.add(lblColumn);
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		lotManager.setConstraints(lblCell, gbcConstraint);
		content.add(lblCell);
		gbcConstraint.gridwidth = 1;
		lotManager.setConstraints(pnlStudent, gbcConstraint);
		content.add(pnlStudent);
		lotManager.setConstraints(pnlAssignment, gbcConstraint);
		content.add(pnlAssignment);
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		lotManager.setConstraints(pnlGrading, gbcConstraint);
		content.add(pnlGrading);
		
		// Add labels and fields to the student (row) panel
		gbcConstraint.weightx = 1;
		gbcConstraint.weighty = 0;
		gbcConstraint.gridwidth = 1;
		gbcConstraint.anchor = GridBagConstraints.NORTH;
		gblStudentManager.setConstraints(lblNumber, gbcConstraint);
		pnlStudent.add(lblNumber);
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		gblStudentManager.setConstraints(txtNumber, gbcConstraint);
		pnlStudent.add(txtNumber);
		gbcConstraint.gridwidth = 1;
		gblStudentManager.setConstraints(lblEmail, gbcConstraint);
		pnlStudent.add(lblEmail);
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		gblStudentManager.setConstraints(txtEmail, gbcConstraint);
		pnlStudent.add(txtEmail);

		// Add labels to the assignment (column) panel
		gbcConstraint.gridwidth = 1;
		gblAssignmentManager.setConstraints(lblAssignment, gbcConstraint);
		pnlAssignment.add(lblAssignment);
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		gblAssignmentManager.setConstraints(txtAssignment, gbcConstraint);
		pnlAssignment.add(txtAssignment);
		gbcConstraint.gridwidth = 1;
		gblAssignmentManager.setConstraints(lblPoints, gbcConstraint);
		pnlAssignment.add(lblPoints);
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		gblAssignmentManager.setConstraints(txtPoints, gbcConstraint);
		pnlAssignment.add(txtPoints);		
		gbcConstraint.gridwidth = 1;
		gblAssignmentManager.setConstraints(lblDueDate, gbcConstraint);
		pnlAssignment.add(lblDueDate);
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		gblAssignmentManager.setConstraints(txtDueDate, gbcConstraint);
		pnlAssignment.add(txtDueDate);
		gbcConstraint.gridwidth = 1;
		gblAssignmentManager.setConstraints(lblCategory, gbcConstraint);
		pnlAssignment.add(lblCategory);
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		gblAssignmentManager.setConstraints(txtCategory, gbcConstraint);
		pnlAssignment.add(txtCategory);
		
		// Add labels to the grade (cell) panel
		gbcConstraint.gridwidth = 1;
		gblGradingManager.setConstraints(lblLate, gbcConstraint);
		pnlGrading.add(lblLate);
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		gblGradingManager.setConstraints(txtLate, gbcConstraint);
		pnlGrading.add(txtLate);
		gbcConstraint.gridwidth = 1;
		gblGradingManager.setConstraints(lblComplete, gbcConstraint);
		pnlGrading.add(lblComplete);
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		gblGradingManager.setConstraints(cbxComplete, gbcConstraint);
		pnlGrading.add(cbxComplete);
		gbcConstraint.gridwidth = 1;
		gblGradingManager.setConstraints(lblInfo, gbcConstraint);
		pnlGrading.add(lblInfo);
		gbcConstraint.gridheight = 2;
		gbcConstraint.weighty = .15;
		gbcConstraint.gridwidth = GridBagConstraints.REMAINDER;
		gblGradingManager.setConstraints(editorScrollPane, gbcConstraint);
		pnlGrading.add(editorScrollPane);
		
		// Make the JFrame visiable
		setVisible(true);		
	}
	
	// Function to keep selection of both tables matched by row
	private void checkSelection(boolean isFixedTable) 
	{
		int fixedSelectedIndex = tblNames.getSelectedRow();
		int selectedIndex = table.getSelectedRow();
		if (fixedSelectedIndex != selectedIndex) 
		{
			if (isFixedTable) 
			{
				table.setRowSelectionInterval(fixedSelectedIndex, fixedSelectedIndex);
				if(intCurCol == -1)
				{
					table.setColumnSelectionInterval(0,0);
				}
			} 
			else 
			{
				tblNames.setRowSelectionInterval(selectedIndex, selectedIndex);
				System.out.println("HERE2!" + selectedIndex);
			}
		}
	}
	
	public static void main(String[] args)
	{
	    try 
		{
			// Set System L&F
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    } 
	    catch (Exception e) 
		{
	       System.out.println(e.toString());
	    }

		Main gui = new Main();
	}
	
	public void mouseClicked(MouseEvent event){}
	public void mouseEntered(MouseEvent event){}
	public void mouseExited(MouseEvent event){}
	public void mousePressed(MouseEvent event){}
	public void mouseReleased(MouseEvent event)
	{
		if(intCurRow > -1)
		{
			cldGradeInfo[intCurRow][intCurCol].setLate(Integer.parseInt(txtLate.getText()));
			if(cbxComplete.getSelectedIndex() == 0)
				cldGradeInfo[intCurRow][intCurCol].setComplete(true);
			else
				cldGradeInfo[intCurRow][intCurCol].setComplete(false);
			cldGradeInfo[intCurRow][intCurCol].setInfo(editorPane.getText());

		}
		if(intCurStu  > - 1)
		{
			sdaStudentInfo[intCurStu].setNumber(txtNumber.getText());
			sdaStudentInfo[intCurStu].setEmail(txtEmail.getText());
		}
		if(intCurCol > -1)
		{
			//txtAssignment, txtPoints, txtDueDate, txtCategory
			asdAssignmentInfo[intCurCol].setAssignment(txtAssignment.getText());
			asdAssignmentInfo[intCurCol].setPoints(txtPoints.getText());
			asdAssignmentInfo[intCurCol].setDueDate(txtDueDate.getText());
			asdAssignmentInfo[intCurCol].setCategory(txtCategory.getText());
		}
		
		// NOTE: Make it so the currently selected column in one table is always the selected column in another table!!!
		// This way the student data can always be available for each assisgnment.  As you change rows in teh main table
		// the student number and email shoudl change to that of the corresponding row in the names table.
		
		intCurStu = tblNames.getSelectedRow();
		intCurRow = table.getSelectedRow();
		intCurCol = table.getSelectedColumn();
		
		if(intCurRow > -1)
		{
			txtLate.setText("" + cldGradeInfo[intCurRow][intCurCol].getLate());
			if(cldGradeInfo[intCurRow][intCurCol].getComplete() == true)
				cbxComplete.setSelectedIndex(0);
			else
				cbxComplete.setSelectedIndex(1);
			editorPane.setText(cldGradeInfo[intCurRow][intCurCol].getInfo());
		}
		if(intCurStu > -1)
		{
			txtNumber.setText(sdaStudentInfo[intCurStu].getNumber());
			txtEmail.setText(sdaStudentInfo[intCurStu].getEmail());
		}
		if(intCurCol > -1)
		{
			txtAssignment.setText(asdAssignmentInfo[intCurCol].getAssignment());
			txtPoints.setText(asdAssignmentInfo[intCurCol].getPoints());
			txtDueDate.setText(asdAssignmentInfo[intCurCol].getDueDate());
			txtCategory.setText(asdAssignmentInfo[intCurCol].getCategory());	
		}
		
		//tblNames.getSelectionModel().setLeadSelectionIndex(table.getSelectionModel().getLeadSelectionIndex());
		//tblNames.getSelectionModel().setAnchorSelectionIndex(table.getSelectionModel().getAnchorSelectionIndex());
//		Graphics g = getGraphics();
//			update(g);
		//System.out.println(tblNames.getSelectionModel().getAnchorSelectionIndex());
	//	System.out.println(tblNames.getSelectionModel().getLeadSelectionIndex());
		//System.out.println(table.getSelectionModel().getAnchorSelectionIndex());
	//	System.out.println(table.getSelectionModel().getLeadSelectionIndex() + "\n");
	//	System.out.println(table.getCellSelectionEnabled());
		
		
	}
	public void actionPerformed(ActionEvent event)
	{
	
		if(event.getActionCommand().equals("Exit"))
		{
			System.exit(0);
		}
		if(event.getActionCommand().equals("Save"))
		{
			if(intCurRow != -1 && intCurCol != -1)
			{
				cldGradeInfo[intCurRow][intCurCol].setLate(Integer.parseInt(txtLate.getText()));
				if(cbxComplete.getSelectedIndex() == 0)
					cldGradeInfo[intCurRow][intCurCol].setComplete(true);
				else
					cldGradeInfo[intCurRow][intCurCol].setComplete(false);
				cldGradeInfo[intCurRow][intCurCol].setInfo(editorPane.getText());
			}
			if(intCurStu  > - 1)
			{
				sdaStudentInfo[intCurStu].setNumber(txtNumber.getText());
				sdaStudentInfo[intCurStu].setEmail(txtEmail.getText());
			}
			if(intCurCol > -1)
			{
				//txtAssignment, txtPoints, txtDueDate, txtCategory
				asdAssignmentInfo[intCurCol].setAssignment(txtAssignment.getText());
				asdAssignmentInfo[intCurCol].setPoints(txtPoints.getText());
				asdAssignmentInfo[intCurCol].setDueDate(txtDueDate.getText());
				asdAssignmentInfo[intCurCol].setCategory(txtCategory.getText());
			}
			if(strSaveFile.length() > 0)
			{
				try
				{
					xmlSave.createXML(strSaveFile);
				}
				catch(Exception e)
				{
					System.out.println(e.toString());
				}
			}
			else
			{
				fc.addChoosableFileFilter(new XMLFilter());
	            fc.setAcceptAllFileFilterUsed(false);
				int returnVal = fc.showSaveDialog(pnlMain);

				if(returnVal == JFileChooser.APPROVE_OPTION)
				{
					try
					{
						File file = fc.getSelectedFile();
						System.out.println(file.getAbsolutePath());
						System.out.println(filOps.getExtension(file));
						if(filOps.getExtension(file).equals("xml") == false)
						{
							File newFile = new File(file.getAbsolutePath() + ".xml");
							System.out.println(newFile.getAbsolutePath());
							xmlSave.createXML(newFile.getAbsolutePath());
							strSaveFile = newFile.getAbsolutePath();
						}
						else
						{
							xmlSave.createXML(file.getAbsolutePath());
							strSaveFile = file.getAbsolutePath();
						}
					}
					catch (Exception e)
					{
						System.err.println(e.toString());
					}
				}
			}
		}
		if(event.getActionCommand().equals("LoadClass"))
		{
			try
			{
                String strClassName = (String)JOptionPane.showInputDialog(this, "Class name:", "Load Class", JOptionPane.PLAIN_MESSAGE);
				Class C = Class.forName(strClassName);
				Test T = (Test)C.newInstance();
				String strArguments = (String)JOptionPane.showInputDialog(this, "Arguments:", "Load Class", JOptionPane.PLAIN_MESSAGE);
				String[] arrArguments = strArguments.split(" ");
				T.main(arrArguments);
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
			/*calculator = new GradeCalculator(table, tblNames, cldGradeInfo, asdAssignmentInfo, sdaStudentInfo);
			calculator.drop("HW 1");
			calculator.drop("Q 1");
			calculator.drop("Q 2");
			calculator.weight("homework", "20");
			calculator.weight("quiz", "40");
			calculator.weight("exam", "40");
			JOptionPane.showMessageDialog(this, calculator.calculate()); */
		}
		if(event.getActionCommand().equals("Save As"))
		{
			if(intCurRow != -1 && intCurCol != -1)
			{
				cldGradeInfo[intCurRow][intCurCol].setLate(Integer.parseInt(txtLate.getText()));
				if(cbxComplete.getSelectedIndex() == 0)
					cldGradeInfo[intCurRow][intCurCol].setComplete(true);
				else
					cldGradeInfo[intCurRow][intCurCol].setComplete(false);
				cldGradeInfo[intCurRow][intCurCol].setInfo(editorPane.getText());
			}
			if(intCurStu  > - 1)
			{
				sdaStudentInfo[intCurStu].setNumber(txtNumber.getText());
				sdaStudentInfo[intCurStu].setEmail(txtEmail.getText());
			}
			if(intCurCol > -1)
			{
				//txtAssignment, txtPoints, txtDueDate, txtCategory
				asdAssignmentInfo[intCurCol].setAssignment(txtAssignment.getText());
				asdAssignmentInfo[intCurCol].setPoints(txtPoints.getText());
				asdAssignmentInfo[intCurCol].setDueDate(txtDueDate.getText());
				asdAssignmentInfo[intCurCol].setCategory(txtCategory.getText());
			}
		
			fc.addChoosableFileFilter(new XMLFilter());
            fc.setAcceptAllFileFilterUsed(false);
			int returnVal = fc.showSaveDialog(pnlMain);

			if(returnVal == JFileChooser.APPROVE_OPTION)
			{
				try
				{
					File file = fc.getSelectedFile();
					System.out.println(file.getAbsolutePath());
					System.out.println(filOps.getExtension(file));
					if(filOps.getExtension(file).equals("xml") == false)
					{
						File newFile = new File(file.getAbsolutePath() + ".xml");
						System.out.println(newFile.getAbsolutePath());
						xmlSave.createXML(newFile.getAbsolutePath());
						strSaveFile = newFile.getAbsolutePath();
					}
					else
					{
						xmlSave.createXML(file.getAbsolutePath());
						strSaveFile = file.getAbsolutePath();
					}
				}
				catch (Exception e)
				{
					System.err.println(e.toString());
				}
			}
		}
		if(event.getActionCommand().equals("Open"))
		{
			fc.addChoosableFileFilter(new XMLFilter());
            fc.setAcceptAllFileFilterUsed(false);
			int returnVal = fc.showOpenDialog(pnlMain);
			if(returnVal == JFileChooser.APPROVE_OPTION)
			{
				try
				{
					File file = fc.getSelectedFile();
					xmlSave.loadXML(file.getAbsolutePath());
					strSaveFile = file.getAbsolutePath();
				}
				catch(Exception e)
				{
					System.err.println("Exception: " + e.toString());
				
				}
			}
			Graphics g = getGraphics();
			update(g);
		}
	}
}